package com.ibm.spatiotemp.server.datasources;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.fasterxml.jackson.databind.JsonNode;
import com.ibm.spatiotemp.server.constants.Constant;
import com.ibm.spatiotemp.tools.jsonParser;
import com.ibm.spatiotemp.utils.JSonInterface;
/*just a data class*/
public class DataSource extends JSonInterface{

	/*todo add the ability to building dsrc instance from and to json format*/
	
	public static Logger logger = Logger.getLogger(DataSource.class);

	/*name of the datasource provided by user*/
	protected String srcname;
	/*db class: informix, mysql, mocking*/
	protected String source_class;
	/*db connection qutencials*/
	protected String connection_string;
	
	/*mode=indexonly,indexwithdata*/
	protected int accessmode = Constant.DBACCESSMODE_IDXONLY;

	protected DataSchema dataschema;
	
	public int getAccessmode() {
		return accessmode;
	}

	public void setAccessmode(int accessmode) {
		this.accessmode = accessmode;
	}
	
	public String getSrcname() {
		return srcname;
	}

	public void setSrcname(String srcname) {
		this.srcname = srcname;
	}
	
	public String getSource_class() {
		return source_class;
	}

	public void setSource_class(String source_class) {
		this.source_class = source_class;
	}

	public String getConnection_string() {
		return connection_string;
	}

	public void setConnection_string(String connection_string) {
		this.connection_string = connection_string;
	}

	public DataSchema getDataschema() {
		return dataschema;
	}

	public void setDataschema(DataSchema dataschema) {
		this.dataschema = dataschema;
	}

	@Override
	public void buildInstanceByJsonString(String jsonstr) {
		JsonNode node = jsonParser.fromString(jsonstr);
		buildInstanceByJsonString(node);		
	}

	@Override
	public void buildInstanceByJsonString(JsonNode node) {
		Class<?> classType = this.getClass();
        Field fields[] = classType.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            Field field = fields[i];
            String fieldName = field.getName();
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
        // 鑾峰緱鍜屽睘鎬у搴旂殑setXXX()鏂规硶
        Method setMethod;
        JsonNode valuenode = node.get(fieldName);
        Object value = null;
        if(valuenode!=null){
        	if(valuenode.isContainerNode()){
        		DataSchema schemaobj = new DataSchema();
        		schemaobj.buildInstanceByJsonString(valuenode);
        		value = schemaobj;
        	}else{
                value = (Object) valuenode.asText();
        	}
        }
        String setMethodName = "set" + firstLetter + fieldName.substring(1);
        try {
        	if(value != null){
			setMethod = classType.getMethod(setMethodName, new Class[]{field.getType()});
			if(value instanceof String){
				if(StringUtils.isNumeric((String)value)){
			        setMethod.invoke(this, new Object[]{Integer.parseInt((String)value)});
				}
				else{
			        setMethod.invoke(this, new Object[]{value});
				}
			}
			else{/*jsoninterface object*/
				setMethod.invoke(this, new Object[]{value});
			}
        	}
		} catch (NoSuchMethodException e) {
//			e.printStackTrace();/*just ignore if attribute is not found*/
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
        
        }		
	}

	@Override
	public String toJsonFormat() {
		Map<String,Object> instmap = ClassReflectionToolkit.getInstancesFields(this);
		return ClassReflectionToolkit.mapToJsonString(instmap);
	}

}
